﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Pyramid.Hardware
{
    public class Decoder
    {
        private OutputLine[] _outputBus;
        private ILine _inputLine;

        public Decoder(ILine inputLine)
        {
            _inputLine = inputLine;
            _inputLine.SignalChange += new EventHandler<SignalChangeEventArgs>(OnInputChangedHandler);

            _outputBus = new OutputLine[]
            {
                new OutputLine(),
                new OutputLine(),
                new OutputLine()
            };

            UpdateOutputBus();
        }

        public IEnumerable<ILine> OutputBus
        {
            get
            {
                return _outputBus;
            }
        }


        private void OnInputChangedHandler(object sender, SignalChangeEventArgs e)
        {
            UpdateOutputBus();
        }

        private void UpdateOutputBus()
        {
            //assumption: signal is false if not selected, true if it is

            _outputBus[0].SetOutput(_inputLine.Signal == Trivalent.False);
            _outputBus[1].SetOutput(_inputLine.Signal == Trivalent.Unknown);
            _outputBus[2].SetOutput(_inputLine.Signal == Trivalent.True);
        }
    }



}
